const fs = require('fs');
const util = require('util');
const path = require('path');

// 读取配置文件
const configPath = path.join(__dirname, '../../public/config.json'); // 修改路径
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));

const logFile = fs.createWriteStream(config.loggerPath, { flags: 'a' });
const logStdout = process.stdout;

function getTimestamp() {
    const now = new Date();
    return now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false });
}

console.log = function () {
    const timestamp = getTimestamp();
    const message = util.format.apply(null, arguments);
    logFile.write(`[${timestamp}] ${message}\n`);
    logStdout.write(`[${timestamp}] ${message}\n`);
}

console.error = function () {
    const timestamp = getTimestamp();
    const message = util.format.apply(null, arguments);
    logFile.write(`[${timestamp}] ${message}\n`);
    logStdout.write(`[${timestamp}] ${message}\n`);
}